<?php
require_once dirname(__FILE__).'/../../bootstrap/functional.php';
require_once dirname(__FILE__).'/../../../../lib/test/WildlivePHPUnitBaseFunctionalTestCase.class.php';

class functional_backend_memberActionsTest extends WildlivePHPUnitBaseFunctionalTestCase
{
  protected function getApplication()
  {
    return 'backend';
  }
  
  public function testFormNewWithAllFieldsAndOneAddressAndOnePhoneNumber() {
    // Get table entries
    $num_members = count(MemberTable::getInstance()->findAll());
    $num_personal_member_data = count(PersonalMemberDataTable::getInstance()->findAll());
    $num_organizational_member_data = count(OrganizationalMemberDataTable::getInstance()->findAll());
    $num_phone_numbers = count(PhoneNumberTable::getInstance()->findAll());
    $num_addesses = count(AddressTable::getInstance()->findAll());
    $num_member_phone_numbers = count(MemberPhoneNumberTable::getInstance()->findAll());
    $num_member_addresses = count(MemberAddressTable::getInstance()->findAll());
    $num_bank_accounts = count(BankAccountTable::getInstance()->findAll());
    
    // Prepare form data
    $gen = new PasswordGenerator();
    $first_name = $gen->generate();
    $last_name = $gen->generate();
    $gender = 'male';
    $od_date_of_joining = '';
    $od_date_of_resignation = '';
    $od_age_group_id = '1';
    $od_organizational_unit_id = '16';
    $od_woggle_id = '';
    $pd_date_of_birth = '';
    $pd_birthplace = '';
    $pd_email_address = '';
    $pd_ad[0] = array(
      'street' => '2345',
      'street_number' => '2345',
      'zip_code' => '12345',
      'city' => 'dgfgf',
      'country_id' => '1');
    $pd_pn[0] = array(
      'area_code' => '1234',
      'call_through' => '567');
    $pd_ba = array(
      'owner' => 'wert',
      'number' => 'tgr',
      'bank_code' => 'uj',
      'bank' => 'hyhh');
    
    // Initialize some stuff
    $routing = $this->getRouting();
    $unit = $this->getOrganizationalUnit();
    
    // Fill form and submit
    $browser = $this->getBrowser();
    $browser->info(__METHOD__);
    $browser = $this->signIn($browser);
    $browser->get($routing->generate('member_new', array('sf_subject' => new Member(), 'unit' => $unit)))
      ->with('request')->begin()
        ->isParameter('module', 'member')
        ->isParameter('action', 'new')
      ->end()
      ->with('response')->begin()
        ->isStatusCode(200)
      ->end()
      ->click('submit', array(
        'member' => array(
          'id' => '',
          'first_name' => $first_name,
          'last_name' => $last_name,
          'gender' => $gender,
          'OrganizationalData' => array(
            'id' => '',
            'date_of_joining' => $od_date_of_joining,
            'date_of_resignation' => $od_date_of_resignation,
            'age_group_id' => $od_age_group_id,
            'organizational_unit_id' => $od_organizational_unit_id,
            'woggle_id' => $od_woggle_id
            ),
          'PersonalData' => array(
            'id' => '',
            'date_of_birth' => $pd_date_of_birth,
            'birthplace' => $pd_birthplace,
            'email_address' => $pd_email_address,
            'Addresses' => array(
              array(
                'id' => '',
                'street' => $pd_ad[0]['street'],
                'street_number' => $pd_ad[0]['street_number'],
                'zip_code' => $pd_ad[0]['zip_code'],
                'city' => $pd_ad[0]['city'],
                'country_id' => $pd_ad[0]['country_id'])
            ),
            'PhoneNumbers' => array(
              array(
                'id' => '',
                'area_code' => $pd_pn[0]['area_code'],
                'call_through' => $pd_pn[0]['call_through'])
            ),
            'BankAccount' => array(
              'id' => '',
              'owner' => $pd_ba['owner'],
              'number' => $pd_ba['number'],
              'bank_code' => $pd_ba['bank_code'],
              'bank' => $pd_ba['bank']
            )))))
      ->info('Form submitted')
      ->with('request')->begin()
        ->isParameter('module', 'member')
        ->isParameter('action', 'create')
      ->end()
      ->with('response')->begin()
        //->isStatusCode(500)
      ->end()
      ->with('form')->begin()
        ->hasErrors(false)
      ->end()
      ;
      
      // Check number of table entries
      $this->assertEquals(count(MemberTable::getInstance()->findAll()), $num_members+1);
      $this->assertEquals($num_personal_member_data+1, count(PersonalMemberDataTable::getInstance()->findAll()));
      $this->assertEquals($num_organizational_member_data+1, count(OrganizationalMemberDataTable::getInstance()->findAll()));
      $this->assertEquals($num_phone_numbers+1, count(PhoneNumberTable::getInstance()->findAll()));
      $this->assertEquals($num_addesses+1, count(AddressTable::getInstance()->findAll()));
      $this->assertEquals($num_member_phone_numbers+1, count(MemberPhoneNumberTable::getInstance()->findAll()));
      $this->assertEquals($num_member_addresses+1, count(MemberAddressTable::getInstance()->findAll()));
      $this->assertEquals($num_bank_accounts+1, count(BankAccountTable::getInstance()->findAll()));
      
      // Check Member object
      $member = MemberTable::getInstance()->findOneByFirstNameAndLastName($first_name, $last_name);
      $this->assertInstanceOf('Member', $member);
      $this->assertEquals($member->getFirstName(), $first_name);
      $this->assertEquals($member->getLastName(), $last_name);
      $this->assertEquals($member->getGender(), $gender);
      
      // Check PersonalMemberData object
      $pd = PersonalMemberDataTable::getInstance()->findOneByMemberId($member->getId());
      $this->assertInstanceOf('PersonalMemberData', $pd);
      //$this->assertEquals($pd->getDateOfBirth(), $date_of_birth)
      $this->assertEquals($pd->getBirthplace(), $pd_birthplace);
      $this->assertEquals($pd->getEmailAddress(), $pd_email_address);
      $this->assertGreaterThan(0, $pd->getBankAccountId()); // Bank Account existent check
      
      // Check OrganizationalMemberData object
      $od = OrganizationalMemberDataTable::getInstance()->findOneByMemberId($member->getId());
      $this->assertInstanceOf('OrganizationalMemberData', $od);
      
      // Check MemberAddress link and Address objects
      $member_addresses = MemberAddressTable::getInstance()->findByPersonalMemberDataId($pd->getId());
      $this->assertInstanceOf('Doctrine_Collection', $member_addresses);
      $this->assertEquals(count($member_addresses), count($pd_ad));
      foreach($member_addresses as $member_address) {
        $address = AddressTable::getInstance()->findOneById($member_address->getAddressId());
        $this->assertInstanceOf('Address', $address);
      }
      
      // Check MemberPhoneNumber link and PhoneNumber objects
      $member_phone_numbers = MemberPhoneNumberTable::getInstance()->findByMemberId($pd->getId());
      $this->assertInstanceOf('Doctrine_Collection', $member_phone_numbers);
      $this->assertEquals(count($member_phone_numbers), count($pd_pn));
      foreach($member_phone_numbers as $member_phone_number) {
        $phone_number = PhoneNumberTable::getInstance()->findOneById($member_phone_number->getPhoneNumberId());
        $this->assertInstanceOf('PhoneNumber', $phone_number);
      }
       
      // Check BankAccount object
      $ba = BankAccountTable::getInstance()->findOneById($pd->getBankAccountId());
      $this->assertInstanceOf('BankAccount', $ba);
  }
  
  public function testFormNewWithAllFieldsAndTwoAddressAndOnePhoneNumber() {
    // Get table entries
    $num_members = count(MemberTable::getInstance()->findAll());
    $num_personal_member_data = count(PersonalMemberDataTable::getInstance()->findAll());
    $num_organizational_member_data = count(OrganizationalMemberDataTable::getInstance()->findAll());
    $num_phone_numbers = count(PhoneNumberTable::getInstance()->findAll());
    $num_addesses = count(AddressTable::getInstance()->findAll());
    $num_member_phone_numbers = count(MemberPhoneNumberTable::getInstance()->findAll());
    $num_member_addresses = count(MemberAddressTable::getInstance()->findAll());
    $num_bank_accounts = count(BankAccountTable::getInstance()->findAll());
    
    // Prepare form data
    $gen = new PasswordGenerator();
    $first_name = $gen->generate();
    $last_name = $gen->generate();
    $gender = 'male';
    $od_date_of_joining = '';
    $od_date_of_resignation = '';
    $od_age_group_id = '1';
    $od_organizational_unit_id = '16';
    $od_woggle_id = '';
    $pd_date_of_birth = '';
    $pd_birthplace = '';
    $pd_email_address = '';
    $pd_ad[0] = array(
      'street' => '2345',
      'street_number' => '2345',
      'zip_code' => '12345',
      'city' => 'dgfgf',
      'country_id' => '1');
    $pd_pn[0] = array(
      'area_code' => '1234',
      'call_through' => '567');
    $pd_pn[1] = array(
      'area_code' => '9876',
      'call_through' => '54754');
    $pd_ba = array(
      'owner' => 'wert',
      'number' => 'tgr',
      'bank_code' => 'uj',
      'bank' => 'hyhh');
    
    // Initialize some stuff
    $routing = $this->getRouting();
    $unit = $this->getOrganizationalUnit();
    
    // Fill form and submit
    $browser = $this->getBrowser();
    $browser->info(__METHOD__);
    $browser = $this->signIn($browser);
    $browser->get($routing->generate('member_new', array('sf_subject' => new Member(), 'unit' => $unit)))
      ->with('request')->begin()
        ->isParameter('module', 'member')
        ->isParameter('action', 'new')
      ->end()
      ->with('response')->begin()
        ->isStatusCode(200)
      ->end()
      ->click('submit', array(
        'member' => array(
          'id' => '',
          'first_name' => $first_name,
          'last_name' => $last_name,
          'gender' => $gender,
          'OrganizationalData' => array(
            'id' => '',
            'date_of_joining' => $od_date_of_joining,
            'date_of_resignation' => $od_date_of_resignation,
            'age_group_id' => $od_age_group_id,
            'organizational_unit_id' => $od_organizational_unit_id,
            'woggle_id' => $od_woggle_id
            ),
          'PersonalData' => array(
            'id' => '',
            'date_of_birth' => $pd_date_of_birth,
            'birthplace' => $pd_birthplace,
            'email_address' => $pd_email_address,
            'Addresses' => array(
              array(
                'id' => '',
                'street' => $pd_ad[0]['street'],
                'street_number' => $pd_ad[0]['street_number'],
                'zip_code' => $pd_ad[0]['zip_code'],
                'city' => $pd_ad[0]['city'],
                'country_id' => $pd_ad[0]['country_id'])
            ),
            'PhoneNumbers' => array(
              array(
                'id' => '',
                'area_code' => $pd_pn[0]['area_code'],
                'call_through' => $pd_pn[0]['call_through']),
              array(
                'id' => '',
                'area_code' => $pd_pn[1]['area_code'],
                'call_through' => $pd_pn[1]['call_through'])
            ),
            'BankAccount' => array(
              'id' => '',
              'owner' => $pd_ba['owner'],
              'number' => $pd_ba['number'],
              'bank_code' => $pd_ba['bank_code'],
              'bank' => $pd_ba['bank']
            )))))
      ->info('Form submitted')
      ->with('request')->begin()
        ->isParameter('module', 'member')
        ->isParameter('action', 'create')
      ->end()
      ->with('response')->begin()
        //->isStatusCode(500)
      ->end()
      ->with('form')->begin()
        ->hasErrors(false)
      ->end()
      ;
      
      // Check number of table entries
      $this->assertEquals(count(MemberTable::getInstance()->findAll()), $num_members+1);
      $this->assertEquals($num_personal_member_data+1, count(PersonalMemberDataTable::getInstance()->findAll()));
      $this->assertEquals($num_organizational_member_data+1, count(OrganizationalMemberDataTable::getInstance()->findAll()));
      $this->assertEquals($num_phone_numbers+2, count(PhoneNumberTable::getInstance()->findAll()));
      $this->assertEquals($num_addesses+1, count(AddressTable::getInstance()->findAll()));
      $this->assertEquals($num_member_phone_numbers+2, count(MemberPhoneNumberTable::getInstance()->findAll()));
      $this->assertEquals($num_member_addresses+1, count(MemberAddressTable::getInstance()->findAll()));
      $this->assertEquals($num_bank_accounts+1, count(BankAccountTable::getInstance()->findAll()));
      
      // Check Member object
      $member = MemberTable::getInstance()->findOneByFirstNameAndLastName($first_name, $last_name);
      $this->assertInstanceOf('Member', $member);
      $this->assertEquals($member->getFirstName(), $first_name);
      $this->assertEquals($member->getLastName(), $last_name);
      $this->assertEquals($member->getGender(), $gender);
      
      // Check PersonalMemberData object
      $pd = PersonalMemberDataTable::getInstance()->findOneByMemberId($member->getId());
      $this->assertInstanceOf('PersonalMemberData', $pd);
      //$this->assertEquals($pd->getDateOfBirth(), $date_of_birth)
      $this->assertEquals($pd->getBirthplace(), $pd_birthplace);
      $this->assertEquals($pd->getEmailAddress(), $pd_email_address);
      $this->assertGreaterThan(0, $pd->getBankAccountId()); // Bank Account existent check
      
      // Check OrganizationalMemberData object
      $od = OrganizationalMemberDataTable::getInstance()->findOneByMemberId($member->getId());
      $this->assertInstanceOf('OrganizationalMemberData', $od);
      
      // Check MemberAddress link and Address objects
      $member_addresses = MemberAddressTable::getInstance()->findByPersonalMemberDataId($pd->getId());
      $this->assertInstanceOf('Doctrine_Collection', $member_addresses);
      $this->assertEquals(count($member_addresses), count($pd_ad));
      foreach($member_addresses as $member_address) {
        $address = AddressTable::getInstance()->findOneById($member_address->getAddressId());
        $this->assertInstanceOf('Address', $address);
      }
      
      // Check MemberPhoneNumber link and PhoneNumber objects
      $member_phone_numbers = MemberPhoneNumberTable::getInstance()->findByMemberId($pd->getId());
      $this->assertInstanceOf('Doctrine_Collection', $member_phone_numbers);
      $this->assertEquals(count($member_phone_numbers), count($pd_pn));
      foreach($member_phone_numbers as $member_phone_number) {
        $phone_number = PhoneNumberTable::getInstance()->findOneById($member_phone_number->getPhoneNumberId());
        $this->assertInstanceOf('PhoneNumber', $phone_number);
      }
       
      // Check BankAccount object
      $ba = BankAccountTable::getInstance()->findOneById($pd->getBankAccountId());
      $this->assertInstanceOf('BankAccount', $ba);
  }
  
  public function testFormNewWithEmptyAddressFields() {
    // Get table entries
    $num_members = count(MemberTable::getInstance()->findAll());
    $num_personal_member_data = count(PersonalMemberDataTable::getInstance()->findAll());
    $num_organizational_member_data = count(OrganizationalMemberDataTable::getInstance()->findAll());
    $num_phone_numbers = count(PhoneNumberTable::getInstance()->findAll());
    $num_addesses = count(AddressTable::getInstance()->findAll());
    $num_member_phone_numbers = count(MemberPhoneNumberTable::getInstance()->findAll());
    $num_member_addresses = count(MemberAddressTable::getInstance()->findAll());
    $num_bank_accounts = count(BankAccountTable::getInstance()->findAll());
    
    // Prepare form data
    $gen = new PasswordGenerator();
    $first_name = $gen->generate();
    $last_name = $gen->generate();
    $gender = 'male';
    $od_date_of_joining = '';
    $od_date_of_resignation = '';
    $od_age_group_id = '1';
    $od_organizational_unit_id = '16';
    $od_woggle_id = '';
    $pd_date_of_birth = '';
    $pd_birthplace = '';
    $pd_email_address = '';
    $pd_ad[0] = array(
      'street' => '',
      'street_number' => '',
      'zip_code' => '',
      'city' => '',
      'country_id' => '1');
    $pd_pn[0] = array(
      'area_code' => '1234',
      'call_through' => '567');
    $pd_ba = array(
      'owner' => 'wert',
      'number' => 'tgr',
      'bank_code' => 'uj',
      'bank' => 'hyhh');
    
    // Initialize some stuff
    $routing = $this->getRouting();
    $unit = $this->getOrganizationalUnit();
    
    // Fill form and submit
    $browser = $this->getBrowser();
    $browser->info(__METHOD__);
    $browser = $this->signIn($browser);
    $browser->get($routing->generate('member_new', array('sf_subject' => new Member(), 'unit' => $unit)))
      ->with('request')->begin()
        ->isParameter('module', 'member')
        ->isParameter('action', 'new')
      ->end()
      ->with('response')->begin()
        ->isStatusCode(200)
      ->end()
      ->click('submit', array(
        'member' => array(
          'id' => '',
          'first_name' => $first_name,
          'last_name' => $last_name,
          'gender' => $gender,
          'OrganizationalData' => array(
            'id' => '',
            'date_of_joining' => $od_date_of_joining,
            'date_of_resignation' => $od_date_of_resignation,
            'age_group_id' => $od_age_group_id,
            'organizational_unit_id' => $od_organizational_unit_id,
            'woggle_id' => $od_woggle_id
            ),
          'PersonalData' => array(
            'id' => '',
            'date_of_birth' => $pd_date_of_birth,
            'birthplace' => $pd_birthplace,
            'email_address' => $pd_email_address,
            'Addresses' => array(
              array(
                'id' => '',
                'street' => $pd_ad[0]['street'],
                'street_number' => $pd_ad[0]['street_number'],
                'zip_code' => $pd_ad[0]['zip_code'],
                'city' => $pd_ad[0]['city'],
                'country_id' => $pd_ad[0]['country_id'])
            ),
            'PhoneNumbers' => array(
              array(
                'id' => '',
                'area_code' => $pd_pn[0]['area_code'],
                'call_through' => $pd_pn[0]['call_through'])
            ),
            'BankAccount' => array(
              'id' => '',
              'owner' => $pd_ba['owner'],
              'number' => $pd_ba['number'],
              'bank_code' => $pd_ba['bank_code'],
              'bank' => $pd_ba['bank']
            )))))
      ->info('Form submitted')
      ->with('request')->begin()
        ->isParameter('module', 'member')
        ->isParameter('action', 'create')
      ->end()
      ->with('response')->begin()
        //->isStatusCode(500)
      ->end()
      ->with('form')->begin()
        ->hasErrors(false)
      ->end()
      ;
      
      // Check number of table entries
      $this->assertEquals(count(MemberTable::getInstance()->findAll()), $num_members+1);
      $this->assertEquals($num_personal_member_data+1, count(PersonalMemberDataTable::getInstance()->findAll()));
      $this->assertEquals($num_organizational_member_data+1, count(OrganizationalMemberDataTable::getInstance()->findAll()));
      $this->assertEquals($num_phone_numbers+1, count(PhoneNumberTable::getInstance()->findAll()));
      $this->assertEquals($num_addesses+1, count(AddressTable::getInstance()->findAll()));
      $this->assertEquals($num_member_phone_numbers+1, count(MemberPhoneNumberTable::getInstance()->findAll()));
      $this->assertEquals($num_member_addresses+1, count(MemberAddressTable::getInstance()->findAll()));
      $this->assertEquals($num_bank_accounts+1, count(BankAccountTable::getInstance()->findAll()));
      
      // Check Member object
      $member = MemberTable::getInstance()->findOneByFirstNameAndLastName($first_name, $last_name);
      $this->assertInstanceOf('Member', $member);
      $this->assertEquals($member->getFirstName(), $first_name);
      $this->assertEquals($member->getLastName(), $last_name);
      $this->assertEquals($member->getGender(), $gender);
      
      // Check PersonalMemberData object
      $pd = PersonalMemberDataTable::getInstance()->findOneByMemberId($member->getId());
      $this->assertInstanceOf('PersonalMemberData', $pd);
      //$this->assertEquals($pd->getDateOfBirth(), $date_of_birth)
      $this->assertEquals($pd->getBirthplace(), $pd_birthplace);
      $this->assertEquals($pd->getEmailAddress(), $pd_email_address);
      $this->assertGreaterThan(0, $pd->getBankAccountId()); // Bank Account existent check
      
      // Check OrganizationalMemberData object
      $od = OrganizationalMemberDataTable::getInstance()->findOneByMemberId($member->getId());
      $this->assertInstanceOf('OrganizationalMemberData', $od);
      
      // Check MemberAddress link and Address objects
      $member_addresses = MemberAddressTable::getInstance()->findByPersonalMemberDataId($pd->getId());
      $this->assertInstanceOf('Doctrine_Collection', $member_addresses);
      $this->assertEquals(count($member_addresses), count($pd_ad));
      foreach($member_addresses as $member_address) {
        $address = AddressTable::getInstance()->findOneById($member_address->getAddressId());
        $this->assertInstanceOf('Address', $address);
      }
      
      // Check MemberPhoneNumber link and PhoneNumber objects
      $member_phone_numbers = MemberPhoneNumberTable::getInstance()->findByMemberId($pd->getId());
      $this->assertInstanceOf('Doctrine_Collection', $member_phone_numbers);
      $this->assertEquals(count($member_phone_numbers), count($pd_pn));
      foreach($member_phone_numbers as $member_phone_number) {
        $phone_number = PhoneNumberTable::getInstance()->findOneById($member_phone_number->getPhoneNumberId());
        $this->assertInstanceOf('PhoneNumber', $phone_number);
      }
       
      // Check BankAccount object
      $ba = BankAccountTable::getInstance()->findOneById($pd->getBankAccountId());
      $this->assertInstanceOf('BankAccount', $ba);
  }
  
  public function testFormNewWithEmptyPhoneNumber() {
    // Get table entries
    $num_members = count(MemberTable::getInstance()->findAll());
    $num_personal_member_data = count(PersonalMemberDataTable::getInstance()->findAll());
    $num_organizational_member_data = count(OrganizationalMemberDataTable::getInstance()->findAll());
    $num_phone_numbers = count(PhoneNumberTable::getInstance()->findAll());
    $num_addesses = count(AddressTable::getInstance()->findAll());
    $num_member_phone_numbers = count(MemberPhoneNumberTable::getInstance()->findAll());
    $num_member_addresses = count(MemberAddressTable::getInstance()->findAll());
    $num_bank_accounts = count(BankAccountTable::getInstance()->findAll());
    
    // Prepare form data
    $gen = new PasswordGenerator();
    $first_name = $gen->generate();
    $last_name = $gen->generate();
    $gender = 'male';
    $od_date_of_joining = '';
    $od_date_of_resignation = '';
    $od_age_group_id = '1';
    $od_organizational_unit_id = '16';
    $od_woggle_id = '';
    $pd_date_of_birth = '';
    $pd_birthplace = '';
    $pd_email_address = '';
    $pd_ad[0] = array(
      'street' => '2345',
      'street_number' => '2345',
      'zip_code' => '12345',
      'city' => 'dgfgf',
      'country_id' => '1');
    $pd_pn[0] = array(
      'area_code' => '',
      'call_through' => '');
    $pd_ba = array(
      'owner' => 'wert',
      'number' => 'tgr',
      'bank_code' => 'uj',
      'bank' => 'hyhh');
    
    // Initialize some stuff
    $routing = $this->getRouting();
    $unit = $this->getOrganizationalUnit();
    
    // Fill form and submit
    $browser = $this->getBrowser();
    $browser->info(__METHOD__);
    $browser = $this->signIn($browser);
    $browser->get($routing->generate('member_new', array('sf_subject' => new Member(), 'unit' => $unit)))
      ->with('request')->begin()
        ->isParameter('module', 'member')
        ->isParameter('action', 'new')
      ->end()
      ->with('response')->begin()
        ->isStatusCode(200)
      ->end()
      ->click('submit', array(
        'member' => array(
          'id' => '',
          'first_name' => $first_name,
          'last_name' => $last_name,
          'gender' => $gender,
          'OrganizationalData' => array(
            'id' => '',
            'date_of_joining' => $od_date_of_joining,
            'date_of_resignation' => $od_date_of_resignation,
            'age_group_id' => $od_age_group_id,
            'organizational_unit_id' => $od_organizational_unit_id,
            'woggle_id' => $od_woggle_id
            ),
          'PersonalData' => array(
            'id' => '',
            'date_of_birth' => $pd_date_of_birth,
            'birthplace' => $pd_birthplace,
            'email_address' => $pd_email_address,
            'Addresses' => array(
              array(
                'id' => '',
                'street' => $pd_ad[0]['street'],
                'street_number' => $pd_ad[0]['street_number'],
                'zip_code' => $pd_ad[0]['zip_code'],
                'city' => $pd_ad[0]['city'],
                'country_id' => $pd_ad[0]['country_id'])
            ),
            'PhoneNumbers' => array(
              array(
                'id' => '',
                'area_code' => $pd_pn[0]['area_code'],
                'call_through' => $pd_pn[0]['call_through'])
            ),
            'BankAccount' => array(
              'id' => '',
              'owner' => $pd_ba['owner'],
              'number' => $pd_ba['number'],
              'bank_code' => $pd_ba['bank_code'],
              'bank' => $pd_ba['bank']
            )))))
      ->info('Form submitted')
      ->with('request')->begin()
        ->isParameter('module', 'member')
        ->isParameter('action', 'create')
      ->end()
      ->with('response')->begin()
        //->isStatusCode(500)
      ->end()
      ->with('form')->begin()
        ->hasErrors(false)
      ->end()
      ;
      
      // Check number of table entries
      $this->assertEquals(count(MemberTable::getInstance()->findAll()), $num_members+1);
      $this->assertEquals($num_personal_member_data+1, count(PersonalMemberDataTable::getInstance()->findAll()));
      $this->assertEquals($num_organizational_member_data+1, count(OrganizationalMemberDataTable::getInstance()->findAll()));
      $this->assertEquals($num_phone_numbers, count(PhoneNumberTable::getInstance()->findAll())); // No new phone number
      $this->assertEquals($num_addesses+1, count(AddressTable::getInstance()->findAll()));
      $this->assertEquals($num_member_phone_numbers, count(MemberPhoneNumberTable::getInstance()->findAll())); // No phone number linkage
      $this->assertEquals($num_member_addresses+1, count(MemberAddressTable::getInstance()->findAll()));
      $this->assertEquals($num_bank_accounts+1, count(BankAccountTable::getInstance()->findAll()));
      
      // Check Member object
      $member = MemberTable::getInstance()->findOneByFirstNameAndLastName($first_name, $last_name);
      $this->assertInstanceOf('Member', $member);
      $this->assertEquals($member->getFirstName(), $first_name);
      $this->assertEquals($member->getLastName(), $last_name);
      $this->assertEquals($member->getGender(), $gender);
      
      // Check PersonalMemberData object
      $pd = PersonalMemberDataTable::getInstance()->findOneByMemberId($member->getId());
      $this->assertInstanceOf('PersonalMemberData', $pd);
      //$this->assertEquals($pd->getDateOfBirth(), $date_of_birth)
      $this->assertEquals($pd->getBirthplace(), $pd_birthplace);
      $this->assertEquals($pd->getEmailAddress(), $pd_email_address);
      $this->assertGreaterThan(0, $pd->getBankAccountId()); // Bank Account existent check
      
      // Check OrganizationalMemberData object
      $od = OrganizationalMemberDataTable::getInstance()->findOneByMemberId($member->getId());
      $this->assertInstanceOf('OrganizationalMemberData', $od);
      
      // Check MemberAddress link and Address objects
      $member_addresses = MemberAddressTable::getInstance()->findByPersonalMemberDataId($pd->getId());
      $this->assertInstanceOf('Doctrine_Collection', $member_addresses);
      $this->assertEquals(count($member_addresses), count($pd_ad));
      foreach($member_addresses as $member_address) {
        $address = AddressTable::getInstance()->findOneById($member_address->getAddressId());
        $this->assertInstanceOf('Address', $address);
      }
       
      // Check BankAccount object
      $ba = BankAccountTable::getInstance()->findOneById($pd->getBankAccountId());
      $this->assertInstanceOf('BankAccount', $ba);
  }
  
  public function testFormEditWithAllFieldsAndOneAddressAndAllPhoneNumber() {
    // Get table entries
    $num_members = count(MemberTable::getInstance()->findAll());
    $num_personal_member_data = count(PersonalMemberDataTable::getInstance()->findAll());
    $num_organizational_member_data = count(OrganizationalMemberDataTable::getInstance()->findAll());
    $num_phone_numbers = count(PhoneNumberTable::getInstance()->findAll());
    $num_addesses = count(AddressTable::getInstance()->findAll());
    $num_member_phone_numbers = count(MemberPhoneNumberTable::getInstance()->findAll());
    $num_member_addresses = count(MemberAddressTable::getInstance()->findAll());
    $num_bank_accounts = count(BankAccountTable::getInstance()->findAll());
    
    // Initialize some stuff
    $routing = $this->getRouting();
    $member = MemberTable::getInstance()->findAll()->getFirst();
    if(MemberPhoneNumberTable::getInstance()->findByMemberId($member->getPersonalData()->getId())->count() <= 2) {
      for($i = MemberPhoneNumberTable::getInstance()->findByMemberId($member->getPersonalData()->getId())->count(); $i <= 2; $i++) {
        $pn = new PhoneNumber();
        $pn->setAreaCode(substr(microtime(), 0, 4));
        $pn->setCallThrough(substr(microtime(), 2, 6));
        $pn->save();
        $link = new MemberPhoneNumber();
        $link->PhoneNumber = $pn;
        $link->PersonalMemberData = $member->getPersonalData();
        $link->save();
      }
      $num_phone_numbers = count(PhoneNumberTable::getInstance()->findAll());
      $num_member_phone_numbers = count(MemberPhoneNumberTable::getInstance()->findAll());
    }
    $phone_numbers = $member->getPhoneNumbers();
    $unit = $member->getOrganizationalUnit();
    
    // Prepare form data
    $gen = new PasswordGenerator();
    $first_name = $member->getFirstName();
    $last_name = $gen->generate();
    $gender = 'male';
    $od_date_of_joining = '';
    $od_date_of_resignation = '';
    $od_age_group_id = '1';
    $od_organizational_unit_id = '16';
    $od_woggle_id = '';
    $pd_date_of_birth = '';
    $pd_birthplace = '';
    $pd_email_address = '';
    $pd_ad[0] = array(
      'street' => '2345',
      'street_number' => '2345',
      'zip_code' => '12345',
      'city' => 'dgfgf',
      'country_id' => '1');
    $pd_ba = array(
      'owner' => 'wert',
      'number' => 'tgr',
      'bank_code' => 'uj',
      'bank' => 'hyhh');
    foreach($phone_numbers as $phone_number) {
      $pd_pn[] = array(
        'id' => $phone_number->getId(),
        'area_code' => $phone_number->getAreaCode(),
        'call_through' => $phone_number->getCallThrough());
    }
    
    
    // Fill form and submit
    $browser = $this->getBrowser();
    $browser->info(__METHOD__ .' ('.$member->getId().')');
    $browser = $this->signIn($browser);
    $browser->get($routing->generate('member_edit', array('sf_subject' => $member, 'unit' => $unit)))
      ->with('request')->begin()
        ->isParameter('module', 'member')
        ->isParameter('action', 'edit')
      ->end()
      ->with('response')->begin()
        ->isStatusCode(200)
      ->end()
      ->click('submit', array(
        'member' => array(
          'id' => '',
          'first_name' => $first_name,
          'last_name' => $last_name,
          'gender' => $gender,
          'OrganizationalData' => array(
            'id' => '',
            'date_of_joining' => $od_date_of_joining,
            'date_of_resignation' => $od_date_of_resignation,
            'age_group_id' => $od_age_group_id,
            'organizational_unit_id' => $od_organizational_unit_id,
            'woggle_id' => $od_woggle_id
            ),
          'PersonalData' => array(
            'id' => '',
            'date_of_birth' => $pd_date_of_birth,
            'birthplace' => $pd_birthplace,
            'email_address' => $pd_email_address,
            'Addresses' => array(
              array(
                'id' => '',
                'street' => $pd_ad[0]['street'],
                'street_number' => $pd_ad[0]['street_number'],
                'zip_code' => $pd_ad[0]['zip_code'],
                'city' => $pd_ad[0]['city'],
                'country_id' => $pd_ad[0]['country_id'])
            ),
            'PhoneNumbers' => $pd_pn,
            'BankAccount' => array(
              'id' => '',
              'owner' => $pd_ba['owner'],
              'number' => $pd_ba['number'],
              'bank_code' => $pd_ba['bank_code'],
              'bank' => $pd_ba['bank']
            )))))
      ->info('Form submitted')
      ->with('request')->begin()
        ->isParameter('module', 'member')
        ->isParameter('action', 'update')
      ->end()
      ->with('response')->begin()
        //->isStatusCode(500)
      ->end()
      ->with('form')->begin()
        ->hasErrors(false)
      ->end()
      ;
      
      // Check number of table entries
      $this->assertEquals(count(MemberTable::getInstance()->findAll()), $num_members+0);
      $this->assertEquals($num_personal_member_data+0, count(PersonalMemberDataTable::getInstance()->findAll()));
      $this->assertEquals($num_organizational_member_data+0, count(OrganizationalMemberDataTable::getInstance()->findAll()));
      $this->assertEquals($num_phone_numbers+0, count(PhoneNumberTable::getInstance()->findAll()));
      $this->assertEquals($num_addesses+0, count(AddressTable::getInstance()->findAll()));
      $this->assertEquals($num_member_phone_numbers+0, count(MemberPhoneNumberTable::getInstance()->findAll()));
      $this->assertEquals($num_member_addresses+0, count(MemberAddressTable::getInstance()->findAll()));
      $this->assertEquals($num_bank_accounts+0, count(BankAccountTable::getInstance()->findAll()));
      
      // Check Member object
      $member = MemberTable::getInstance()->findOneByFirstNameAndLastName($first_name, $last_name);
      $this->assertInstanceOf('Member', $member);
      $this->assertEquals($member->getFirstName(), $first_name);
      $this->assertEquals($member->getLastName(), $last_name);
      $this->assertEquals($member->getGender(), $gender);
      
      // Check PersonalMemberData object
      $pd = PersonalMemberDataTable::getInstance()->findOneByMemberId($member->getId());
      $this->assertInstanceOf('PersonalMemberData', $pd);
      //$this->assertEquals($pd->getDateOfBirth(), $date_of_birth)
      $this->assertEquals($pd->getBirthplace(), $pd_birthplace);
      $this->assertEquals($pd->getEmailAddress(), $pd_email_address);
      $this->assertGreaterThan(0, $pd->getBankAccountId()); // Bank Account existent check
      
      // Check OrganizationalMemberData object
      $od = OrganizationalMemberDataTable::getInstance()->findOneByMemberId($member->getId());
      $this->assertInstanceOf('OrganizationalMemberData', $od);
      
      // Check MemberAddress link and Address objects
      $member_addresses = MemberAddressTable::getInstance()->findByPersonalMemberDataId($pd->getId());
      $this->assertInstanceOf('Doctrine_Collection', $member_addresses);
      $this->assertEquals(count($member_addresses), count($pd_ad));
      foreach($member_addresses as $member_address) {
        $address = AddressTable::getInstance()->findOneById($member_address->getAddressId());
        $this->assertInstanceOf('Address', $address);
      }
      
      // Check MemberPhoneNumber link and PhoneNumber objects
      $member_phone_numbers = MemberPhoneNumberTable::getInstance()->findByMemberId($pd->getId());
      $this->assertInstanceOf('Doctrine_Collection', $member_phone_numbers);
      $this->assertEquals(count($member_phone_numbers), count($member->getPhoneNumbers()));//count($pd_pn));
      foreach($member_phone_numbers as $member_phone_number) {
        $phone_number = PhoneNumberTable::getInstance()->findOneById($member_phone_number->getPhoneNumberId());
        $this->assertInstanceOf('PhoneNumber', $phone_number);
      }
       
      // Check BankAccount object
      $ba = BankAccountTable::getInstance()->findOneById($pd->getBankAccountId());
      $this->assertInstanceOf('BankAccount', $ba);
  }
  
  public function testFormEditWithAllFieldsAndOneAddressAndDeleteOnePhoneNumber() {
    // Get table entries
    $num_members = count(MemberTable::getInstance()->findAll());
    $num_personal_member_data = count(PersonalMemberDataTable::getInstance()->findAll());
    $num_organizational_member_data = count(OrganizationalMemberDataTable::getInstance()->findAll());
    $num_phone_numbers = count(PhoneNumberTable::getInstance()->findAll());
    $num_addesses = count(AddressTable::getInstance()->findAll());
    $num_member_phone_numbers = count(MemberPhoneNumberTable::getInstance()->findAll());
    $num_member_addresses = count(MemberAddressTable::getInstance()->findAll());
    $num_bank_accounts = count(BankAccountTable::getInstance()->findAll());
    
    // Initialize some stuff
    $routing = $this->getRouting();
    $member = MemberTable::getInstance()->findAll()->getFirst();
    if(MemberPhoneNumberTable::getInstance()->findByMemberId($member->getPersonalData()->getId())->count() <= 2) {
      for($i = MemberPhoneNumberTable::getInstance()->findByMemberId($member->getPersonalData()->getId())->count(); $i <= 2; $i++) {
        $pn = new PhoneNumber();
        $pn->setAreaCode(substr(microtime(), 0, 4));
        $pn->setCallThrough(substr(microtime(), 2, 6));
        $pn->save();
        $link = new MemberPhoneNumber();
        $link->PhoneNumber = $pn;
        $link->PersonalMemberData = $member->getPersonalData();
        $link->save();
      }
      $num_phone_numbers = count(PhoneNumberTable::getInstance()->findAll());
      $num_member_phone_numbers = count(MemberPhoneNumberTable::getInstance()->findAll());
    }
    $phone_numbers = $member->getPhoneNumbers();
    $unit = $member->getOrganizationalUnit();
    
    // Prepare form data
    $gen = new PasswordGenerator();
    $first_name = $member->getFirstName();
    $last_name = $gen->generate();
    $gender = 'male';
    $od_date_of_joining = '';
    $od_date_of_resignation = '';
    $od_age_group_id = '1';
    $od_organizational_unit_id = '16';
    $od_woggle_id = '';
    $pd_date_of_birth = '';
    $pd_birthplace = '';
    $pd_email_address = '';
    $pd_ad[0] = array(
      'street' => '2345',
      'street_number' => '2345',
      'zip_code' => '12345',
      'city' => 'dgfgf',
      'country_id' => '1');
    $pd_ba = array(
      'owner' => 'wert',
      'number' => 'tgr',
      'bank_code' => 'uj',
      'bank' => 'hyhh');
    foreach($phone_numbers as $phone_number) {
      $pd_pn[] = array(
        'id' => $phone_number->getId(),
        'area_code' => $phone_number->getAreaCode(),
        'call_through' => $phone_number->getCallThrough());
    }
    
    // Remove last phone number
    $last_pn_key = count($pd_pn) - 1;
    $pd_pn[$last_pn_key]['area_code'] = '';
    $pd_pn[$last_pn_key]['call_through'] = '';
    
    // Fill form and submit
    $browser = $this->getBrowser();
    $browser->info(__METHOD__ .' ('.$member->getId().')');
    $browser->info('Deleting phone number id '.$pd_pn[$last_pn_key]['id']);
    $browser = $this->signIn($browser);
    $browser->get($routing->generate('member_edit', array('sf_subject' => $member, 'unit' => $unit)))
      ->with('request')->begin()
        ->isParameter('module', 'member')
        ->isParameter('action', 'edit')
      ->end()
      ->with('response')->begin()
        ->isStatusCode(200)
      ->end()
      ->click('submit', array(
        'member' => array(
          'id' => '',
          'first_name' => $first_name,
          'last_name' => $last_name,
          'gender' => $gender,
          'OrganizationalData' => array(
            'id' => '',
            'date_of_joining' => $od_date_of_joining,
            'date_of_resignation' => $od_date_of_resignation,
            'age_group_id' => $od_age_group_id,
            'organizational_unit_id' => $od_organizational_unit_id,
            'woggle_id' => $od_woggle_id
            ),
          'PersonalData' => array(
            'id' => '',
            'date_of_birth' => $pd_date_of_birth,
            'birthplace' => $pd_birthplace,
            'email_address' => $pd_email_address,
            'Addresses' => array(
              array(
                'id' => '',
                'street' => $pd_ad[0]['street'],
                'street_number' => $pd_ad[0]['street_number'],
                'zip_code' => $pd_ad[0]['zip_code'],
                'city' => $pd_ad[0]['city'],
                'country_id' => $pd_ad[0]['country_id'])
            ),
            'PhoneNumbers' => $pd_pn,
            'BankAccount' => array(
              'id' => '',
              'owner' => $pd_ba['owner'],
              'number' => $pd_ba['number'],
              'bank_code' => $pd_ba['bank_code'],
              'bank' => $pd_ba['bank']
            )))))
      ->info('Form submitted')
      ->with('request')->begin()
        ->isParameter('module', 'member')
        ->isParameter('action', 'update')
      ->end()
      ->with('response')->begin()
        //->isStatusCode(500)
      ->end()
      ->with('form')->begin()
        ->hasErrors(false)
      ->end()
      ;
      
      // Check number of table entries
      $this->assertEquals(count(MemberTable::getInstance()->findAll()), $num_members+0);
      $this->assertEquals($num_personal_member_data+0, count(PersonalMemberDataTable::getInstance()->findAll()));
      $this->assertEquals($num_organizational_member_data+0, count(OrganizationalMemberDataTable::getInstance()->findAll()));
      $this->assertEquals($num_phone_numbers-1, count(PhoneNumberTable::getInstance()->findAll()));
      $this->assertEquals($num_addesses+0, count(AddressTable::getInstance()->findAll()));
      $this->assertEquals($num_member_phone_numbers-1, count(MemberPhoneNumberTable::getInstance()->findAll()));
      $this->assertEquals($num_member_addresses+0, count(MemberAddressTable::getInstance()->findAll()));
      $this->assertEquals($num_bank_accounts+0, count(BankAccountTable::getInstance()->findAll()));
      
      // Check Member object
      $member = MemberTable::getInstance()->findOneByFirstNameAndLastName($first_name, $last_name);
      $this->assertInstanceOf('Member', $member);
      $this->assertEquals($member->getFirstName(), $first_name);
      $this->assertEquals($member->getLastName(), $last_name);
      $this->assertEquals($member->getGender(), $gender);
      
      // Check PersonalMemberData object
      $pd = PersonalMemberDataTable::getInstance()->findOneByMemberId($member->getId());
      $this->assertInstanceOf('PersonalMemberData', $pd);
      //$this->assertEquals($pd->getDateOfBirth(), $date_of_birth)
      $this->assertEquals($pd->getBirthplace(), $pd_birthplace);
      $this->assertEquals($pd->getEmailAddress(), $pd_email_address);
      $this->assertGreaterThan(0, $pd->getBankAccountId()); // Bank Account existent check
      
      // Check OrganizationalMemberData object
      $od = OrganizationalMemberDataTable::getInstance()->findOneByMemberId($member->getId());
      $this->assertInstanceOf('OrganizationalMemberData', $od);
      
      // Check MemberAddress link and Address objects
      $member_addresses = MemberAddressTable::getInstance()->findByPersonalMemberDataId($pd->getId());
      $this->assertInstanceOf('Doctrine_Collection', $member_addresses);
      $this->assertEquals(count($member_addresses), count($pd_ad));
      foreach($member_addresses as $member_address) {
        $address = AddressTable::getInstance()->findOneById($member_address->getAddressId());
        $this->assertInstanceOf('Address', $address);
      }
      
      // Check MemberPhoneNumber link and PhoneNumber objects
      $member_phone_numbers = MemberPhoneNumberTable::getInstance()->findByMemberId($pd->getId());
      $this->assertInstanceOf('Doctrine_Collection', $member_phone_numbers);
      $this->assertEquals(count($member_phone_numbers), count($member->getPhoneNumbers()));//count($pd_pn));
      foreach($member_phone_numbers as $member_phone_number) {
        $phone_number = PhoneNumberTable::getInstance()->findOneById($member_phone_number->getPhoneNumberId());
        $this->assertInstanceOf('PhoneNumber', $phone_number);
      }
       
      // Check BankAccount object
      $ba = BankAccountTable::getInstance()->findOneById($pd->getBankAccountId());
      $this->assertInstanceOf('BankAccount', $ba);
  }
  
  
  protected function getRouting() {
    $routing = $this->getContext()->getRouting();
    $routingOptions = $routing->getOptions();
    $routingOptions['context']['prefix'] = '';
    $routing->initialize(sfContext::getInstance()->getEventDispatcher(), $routing->getCache(), $routingOptions);
    return $routing;
  }
  
  protected function getOrganizationalUnit() {
    $unit = OrganizationalUnitTable::getInstance()->findOneByName('Stamm Silva Abnoba');
    return $unit;
  }
}